DateGenerator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 15
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 11
dl 0
loc 15
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A generate 0 13 2
1
import Generator from './Base';
2
3
/**
4
 * Generates a random date in specified range.
5
 * @returns {date} random date
6
 * @param {date} min minimum possible value
7
 * @param {date} max maximum possible value
8
 * @requires int
9
 * @generator
10
 */
11
12
13
function subtractYear(date) {
14
    const res = new Date();
15
16
    res.setFullYear(date.getFullYear() - 1);
17
18
    return res;
19
}
20
21
export default class DateGenerator extends Generator {
22
    generate(min, max) {
23
        const now = new Date();
24
        const minDate = min || subtractYear(now);
25
        const maxDate = max || now;
26
        const diff = maxDate - minDate;
27
28
        if (diff === 0) return minDate;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
29
        const absDiff = Math.abs(diff);
30
        const rand = this.fatum.int(0, absDiff);
31
        const timestamp = +minDate + absDiff / diff * rand;
32
33
        return new Date(timestamp);
34
    }
35
}
36